/**
 * Project Name:九色鹿
 * File Name:IpUtil.java
 * Package Name:com.fxp.project.jiuselu.commons.ip
 * Date:2021年03月16日 22:48
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.commons.ip;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * ClassName:IpUtil<br/>
 * Function: IpUtil<br/>
 * Date:2021年03月16日 22:48<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK1.8
 */
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = IpUtil.ID_PREFIX)
public class IpUtil {

    public static final String ID_PREFIX = "ipdata";

    private String path;
    private String fileName;

    /**
     * 通过用户的请求获取用户的ip地址和主机信息
     *
     * @param request 用户发送的请求
     * @return String 返回类型  （地址跳转参数）
     * @throws Exception
     */
    public static String getClientIpAddr(HttpServletRequest request) {
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        log.error("IP获取失败了！", e);
                    }
                    assert inet != null;
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) {
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }

    /**
     * getIPSeeker:根据传递的IP地址获取IP信息. <br/>
     *
     * @param ipAddress
     * @return
     * @author Jason.Wang
     * @since JDK 1.7
     */
    public IPLocation getIPSeeker(String ipAddress) {
        if (ipAddress.startsWith("127.0.0.") || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
            IPLocation ipLocation = new IPLocation();
            ipLocation.setCountry("本机地址");
            return ipLocation;
        }
        File file = new File(getPath() + getFileName());
        if (file.exists() && file.isFile() && file.canRead()) {
            IPSeeker ipSeeker = new IPSeeker(getFileName(), getPath());
            return ipSeeker.getIPLocation(ipAddress);
        }
        return null;
    }
}
